package com.example.infowall02.service.impl;

import com.example.infowall02.domain.Articles;
import com.example.infowall02.mapper.ArticlesMapper;
import com.example.infowall02.mapper.FavoriteMapper;
import com.example.infowall02.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;
    @Autowired
    private ArticlesMapper articlesMapper;

    @Override
    public void addFavorite(int userId, int articleId) {
        favoriteMapper.addFavorite(userId, articleId);
        articlesMapper.incrementFavorites(articleId);
    }
    @Override
    public void deleteFavorite(int userId, int articleId) {
        favoriteMapper.deleteFavorite(userId, articleId);
        articlesMapper.decrementFavorites(articleId); // 假设 ArticlesMapper 有减少收藏数的方法
    }
    @Override
    public boolean isUserFavoritedArticle(int userId, int articleId) {
        return favoriteMapper.countFavorites(userId, articleId) > 0;
    }
    @Override
    public List<Articles> getUserFavoriteArticles(int userId) {
        return favoriteMapper.getUserFavoriteArticles(userId);
    }
    @Override
    public int getUserFavoriteCount(int userId) {
        return favoriteMapper.getUserFavoriteCount(userId);
    }

}
